package com.mc.jf.controller;

import com.mc.jf.entity.Plan;
import com.mc.jf.entity.ResUtil;
import com.mc.jf.service.impl.PlanServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lin
 * @since 2023-05-03
 */
@Controller
@RequestMapping("/plan")
public class PlanController {
    @Autowired
    PlanServiceImpl planService;


    @GetMapping(value = "/add")
    public String add() {
        return "plan/add";
    }

    @GetMapping(value = "/edit")
    public String edit(Map<String, Object> map, String id) {
        Plan plan = planService.getById(id);
        map.put("data", plan);
        return "plan/edit";
    }



    @PostMapping("/edit")
    @ResponseBody
    public Object edit(@RequestBody Plan paramplan) {

        Plan plan = planService.getById(paramplan.getId());
        plan.setDate(paramplan.getDate());
        if (!StringUtils.hasLength(paramplan.getEdate())){
            plan.setEdate(paramplan.getDate());
        }else {
            plan.setEdate(paramplan.getEdate());
        }
        plan.setAction(paramplan.getAction());
        plan.setLocation(paramplan.getLocation());
        if (planService.saveOrUpdate(plan)){
            return ResUtil.ok(plan);
        }else {
            return ResUtil.error("操作失败");
        }

    }


    @GetMapping("/del")
    @ResponseBody
    public Object del(String id) {
        if (planService.removeById(id)){
            return ResUtil.ok(null);
        }else {
            return ResUtil.error("操作失败");
        }
    }

    @GetMapping(value = "/index")
    public String list() {
        return "plan/index";
    }

}
